package gw.com.android.app.dispatch;

import android.app.Activity;
import android.text.TextUtils;

import java.util.Map;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.model.ConfigType;
import gw.com.android.model.DataManager;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.MainActivity;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.app.AppActivities;
import www.com.library.app.ObjectSessionStore;
import www.com.library.dialog.ToastPopWindow;
import www.com.library.model.DataItemDetail;

/**
 * @author jett
 * @since 2018-05-16.
 */
public class QuoteDispatch implements Dispatch {

    @Override
    public boolean handle(Activity activity, String path, Map<String, String> params) {
        if ("tab".equals(path)) {
            toQuoteTab();
            return true;
        } else if ("chart".equals(path)) {
            showChart(activity, params);
            return true;
        }
        return false;
    }

    /**
     * hxgts2://quote/tab/
     */
    private void toQuoteTab() {
        AppActivities.getSingleton().popAllActivityExceptOne(MainActivity.class.getSimpleName());
        MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
        if (mainActivity != null) {
            mainActivity.setTabFragment(ConfigType.TAB_QUOTE_TAG, "");
        }
    }

    /**
     * hxgts2://quote/chart?prdName=伦敦银
     */
    private void showChart(Activity activity, Map<String, String> params) {
        if (params == null) {
            new ToastPopWindow(activity, "产品为空").show();
            return;
        }
        String prdName = params.get("prdName");
        if (TextUtils.isEmpty(prdName)) {
            new ToastPopWindow(activity, "产品为空").show();
            return;
        }
        DataItemDetail tickModel = DataManager.instance().getTickModelByName(prdName);
        if (tickModel == null) {
            new ToastPopWindow(activity, "找不到该产品").show();
            return;
        }
        ActivityManager.showChartActivity(activity, tickModel.getInt(GTSConst.JSON_KEY_CODEID), tickModel.getInt(GTSConst.JSON_KEY_ZONE), AppContances.TRADE_TYPE_FORM_HOME);
    }

}
